package com.hertz.appr.pojo;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hertz.appr.http.MyHttpClient;
import com.hertz.appr.http.MyHttpElement;
import com.hertz.appr.http.Parameter;
import com.hertz.appr.http.Response;
import com.hertz.appr.json.JSONArray;
import com.hertz.appr.json.JSONObject;
import com.hertz.appr.oauth.OAuth;
import com.hertz.appr.util.Configuration;

public class User {
	
	private String uid;
	private String nick;
	private String provider;
	private static String qq_user_info_api = "http://open.t.qq.com/api/user/info";
	private static String sina_user_info_api = "http://api.t.sina.com.cn/users/show.json";
	private static Logger log = Logger.getLogger(User.class);
	public static User getUser(OAuth oauth){
		
		if(oauth.getProvider().equals(Configuration.QQ_PROVIDER)){
			MyHttpClient httpClient = new MyHttpClient(oauth.getProvider());
			List<Parameter> parms = new ArrayList<Parameter>();
			MyHttpElement he = oauth.getOauthHttpElement(qq_user_info_api,MyHttpClient.GET, parms);
			try {
				Response response = httpClient.httpRequest(qq_user_info_api, he);
				return buildUser(response,oauth.getProvider());
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}else{
			MyHttpClient httpClient = new MyHttpClient(oauth.getProvider());
			List<Parameter> parms = new ArrayList<Parameter>();
//			parms.add(new Parameter("source", Configuration.getOAuthConsumerKey(oauth.getProvider())));
			MyHttpElement he = oauth.getOauthHttpElement(sina_user_info_api,MyHttpClient.GET, parms);
			try {
				Response response = httpClient.httpRequest(sina_user_info_api, he);
				return buildUser(response,oauth.getProvider());
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static User getUser4Sina(OAuth oauth,String uid){
		
		MyHttpClient httpClient = new MyHttpClient(oauth.getProvider());
		List<Parameter> parms = new ArrayList<Parameter>();
//		parms.add(new Parameter("source", Configuration.getOAuthConsumerKey(oauth.getProvider())));
		parms.add(new Parameter("user_id", uid));
		MyHttpElement he = oauth.getOauthHttpElement(sina_user_info_api,MyHttpClient.GET, parms);
		try {
			Response response = httpClient.httpRequest(sina_user_info_api, he);
			return buildUser(response,oauth.getProvider());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	private static User buildUser(Response response,String provider) throws Exception{
		log.debug("user info : " + response.toString());
		if(provider.equals(Configuration.QQ_PROVIDER)){
			JSONObject jsonObject = response.asJSONObject();
			if(jsonObject.getString("ret") == null || !jsonObject.getString("ret").equals("0")){
				return null;
			}
			JSONObject data = jsonObject.getJSONObject("data");
			if(data.toString().equals("null")){
				return null;
			}
			User user = new User();
			user.uid = data.getString("name");
			user.nick = data.getString("nick");
			return user;
		}else{
			JSONObject jsonObject = response.asJSONObject();
			
			User user = new User();
			user.uid = jsonObject.getString("id");
			user.nick = jsonObject.getString("screen_name");
			return user;
		}
	}
	
	
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getProvider() {
		return provider;
	}
	public void setProvider(String provider) {
		this.provider = provider;
	}

	@Override
	public String toString(){
		return "User{" +
		" provider=" + String.format("%1$4S", provider) +
        ", uid=" + String.format("%1$-20S", uid) +
        ", nick=" + String.format("%1$-20S", this.nick) +
        '}';
	}

}
